﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace water_pipe_repairing
{
    /// <summary>
    /// 游戏类
    /// </summary>
    class Game
    {
        private const int GameHeight = 500;
        private const int GameWidth = 800;

        static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }
        //获取时间种子

        static int map()
        {
            Random ran = new Random(GetRandomSeed());
            int map = ran.Next(1, 5);
            return map;
        }
        //随机获取地图

        public int mapkey = map(); 

        private const int Cols = 9;
        private const int Rows = 4;


        public Random Random = new Random();

      
        private List<Block> Blocks { get; set; }
        /// <summary>
        /// 初始化
        /// </summary>
        public Game()
        {
            Blocks = new List<Block>();
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    
                    Block block = new Block(new Point(i, j), BlockStatus.弯曲水管, Blockdirection.上);
                    //给block初始状态
                    Random random = new Random(GetRandomSeed());
                    int n = random.Next(1, 5);
                    
                    //生成随机数 为后面管子状态随机做准备
                    switch (n)
                    {
                        case 1:
                            block.direction = Blockdirection.上;
                            break;
                        case 2:
                            block.direction = Blockdirection.下;
                            break;
                        case 3:
                            block.direction = Blockdirection.左;
                            break;
                        case 4:
                            block.direction = Blockdirection.右;
                            break;
                    }
                    Blocks.Add(block);
                }
            }           
            switch (mapkey)
            {
                case 1:
                    Blocks[0].status = BlockStatus.阀门; Blocks[0].direction = Blockdirection.关;
                    Blocks[9].status = BlockStatus.墙;
                    Blocks[18].status = BlockStatus.墙;
                    Blocks[27].status = BlockStatus.墙;
                    Blocks[1].status = BlockStatus.直水管; Blocks[1].direction = Blockdirection.竖;
                    Blocks[3].status = BlockStatus.直水管; Blocks[3].direction = Blockdirection.横;
                    Blocks[7].status = BlockStatus.直水管; Blocks[7].direction = Blockdirection.竖;
                    Blocks[10].status = BlockStatus.直水管; Blocks[10].direction = Blockdirection.横;
                    Blocks[12].status = BlockStatus.直水管; Blocks[12].direction = Blockdirection.横;
                    Blocks[16].status = BlockStatus.直水管; Blocks[16].direction = Blockdirection.竖;
                    Blocks[21].status = BlockStatus.直水管; Blocks[21].direction = Blockdirection.横;
                    Blocks[23].status = BlockStatus.直水管; Blocks[23].direction = Blockdirection.横;
                    Blocks[24].status = BlockStatus.直水管; Blocks[24].direction = Blockdirection.横;
                    Blocks[31].status = BlockStatus.直水管; Blocks[31].direction = Blockdirection.横;
                    break;
                case 2:
                    Blocks[0].status = BlockStatus.阀门; Blocks[0].direction = Blockdirection.关;
                    Blocks[9].status = BlockStatus.墙;
                    Blocks[18].status = BlockStatus.墙;
                    Blocks[27].status = BlockStatus.墙;
                    Blocks[3].status = BlockStatus.直水管; Blocks[3].direction = Blockdirection.横;
                    Blocks[7].status = BlockStatus.直水管; Blocks[7].direction = Blockdirection.竖;
                    Blocks[15].status = BlockStatus.直水管; Blocks[15].direction = Blockdirection.横;
                    Blocks[17].status = BlockStatus.直水管; Blocks[17].direction = Blockdirection.横;
                    Blocks[21].status = BlockStatus.直水管; Blocks[21].direction = Blockdirection.横;
                    Blocks[31].status = BlockStatus.直水管; Blocks[31].direction = Blockdirection.横;
                    Blocks[33].status = BlockStatus.直水管; Blocks[33].direction = Blockdirection.横;
                    break;
                case 3:
                    Blocks[0].status = BlockStatus.阀门; Blocks[0].direction = Blockdirection.关;
                    Blocks[9].status = BlockStatus.墙;
                    Blocks[18].status = BlockStatus.墙;
                    Blocks[27].status = BlockStatus.墙;
                    Blocks[1].status = BlockStatus.直水管; Blocks[1].direction = Blockdirection.横;
                    Blocks[2].status = BlockStatus.直水管; Blocks[2].direction = Blockdirection.竖;
                    Blocks[7].status = BlockStatus.直水管; Blocks[7].direction = Blockdirection.横;
                    Blocks[15].status = BlockStatus.直水管; Blocks[15].direction = Blockdirection.竖;
                    Blocks[29].status = BlockStatus.直水管; Blocks[29].direction = Blockdirection.横;
                    Blocks[31].status = BlockStatus.直水管; Blocks[31].direction = Blockdirection.竖;
                    Blocks[33].status = BlockStatus.直水管; Blocks[33].direction = Blockdirection.竖;
                    Blocks[35].status = BlockStatus.直水管; Blocks[35].direction = Blockdirection.横;
                    break;
                case 4:
                    Blocks[0].status = BlockStatus.阀门; Blocks[0].direction = Blockdirection.关;
                    Blocks[9].status = BlockStatus.墙;
                    Blocks[18].status = BlockStatus.墙;
                    Blocks[27].status = BlockStatus.墙;
                    Blocks[3].status = BlockStatus.直水管; Blocks[3].direction = Blockdirection.横;
                    Blocks[7].status = BlockStatus.直水管; Blocks[7].direction = Blockdirection.竖;
                    Blocks[10].status = BlockStatus.直水管; Blocks[10].direction = Blockdirection.横;
                    Blocks[12].status = BlockStatus.直水管; Blocks[12].direction = Blockdirection.竖;
                    Blocks[14].status = BlockStatus.直水管; Blocks[14].direction = Blockdirection.横;
                    Blocks[19].status = BlockStatus.直水管; Blocks[19].direction = Blockdirection.竖;
                    Blocks[20].status = BlockStatus.直水管; Blocks[20].direction = Blockdirection.横;
                    Blocks[23].status = BlockStatus.直水管; Blocks[23].direction = Blockdirection.竖;
                    Blocks[24].status = BlockStatus.直水管; Blocks[24].direction = Blockdirection.横;
                    Blocks[25].status = BlockStatus.直水管; Blocks[25].direction = Blockdirection.竖;
                    Blocks[31].status = BlockStatus.直水管; Blocks[31].direction = Blockdirection.竖;
                    break;

            }

        }

        internal void Draw(object graphics, Size size)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 绘制游戏背景
        /// </summary>
        /// <param name="g">绘图句柄</param>
        /// <param name="rec">游戏尺寸</param>
        public void Draw(Graphics g, Size size)
        {
            
            Image image = new Bitmap(GameWidth, GameHeight);
            Graphics _g = Graphics.FromImage(image);
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));
            _g.DrawImage(Properties.Resources.背景, rec);
            g.DrawImage(image, new Rectangle(new Point(0, 0), size));
            int squarewidth = 100;
            int squareHeight = 100;
            foreach (var block in Blocks)
            {
                Rectangle recblock = new Rectangle();
                recblock.Size = new Size((int)squarewidth, (int)squareHeight);
                recblock.Location = new Point((int)(100 + (block.Point.Y) * squarewidth),
                                            (int)(100 + (block.Point.X) * squareHeight));
                block.draw(g, recblock);
            }
        }

        public Block MouseDown(Point point, Size size)
        {
            float squarewidth = 100;
            float squareHeight = 100;
            foreach (var block in Blocks)
            {
                //当前block的长方形
                Rectangle recblock = new Rectangle();
                {
                    recblock.Size = new Size((int)squarewidth, (int)squareHeight);
                    recblock.Location = new Point((int)(100 + (block.Point.Y) * squarewidth),
                                                (int)(100 + (block.Point.X) * squareHeight));
                };
                if (recblock.Contains(point))
                {
                    //point 这个点在recblock里面
                    switch(block.status)
                    {
                        case BlockStatus.弯曲水管 :
                            switch (block.direction)
                            {
                                case Blockdirection.上:
                                    block.direction = Blockdirection.右;
                                    break;
                                case Blockdirection.右:
                                    block.direction = Blockdirection.下;
                                    break;
                                case Blockdirection.下:
                                    block.direction = Blockdirection.左;
                                    break;
                                case Blockdirection.左:
                                    block.direction = Blockdirection.上;
                                    break;
                            }
                            break;
                        case BlockStatus.直水管:
                            switch (block.direction)
                            {
                                case Blockdirection.横:
                                    block.direction = Blockdirection.竖;
                                    break;
                                case Blockdirection.竖:
                                    block.direction = Blockdirection.横;
                                    break;
                            }
                            break;
                        case BlockStatus.阀门:
                            switch (block.direction)
                            {
                                case Blockdirection.关:
                                    block.direction = Blockdirection.开;
                                    break;
                                case Blockdirection.开:
                                    block.direction = Blockdirection.关;
                                    break;
                            }
                            break;                    
                    }
                    //返回当前blcok
                    return block;
                }

            }
            return null;//没有点击成功
        }

        public bool IsValaveOpen(Block block)
        {
            if (Blocks[0].direction == Blockdirection.开)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 判断阀门是否打开
        /// </summary>
        /// <param name="block"></param>
        /// <returns></returns>

        public bool IsGameOver(Block block)
        {
            switch (mapkey)
            {
                case 1:
                    if (Blocks[1].direction == Blockdirection.横 &
                    Blocks[2].direction == Blockdirection.左 &
                    Blocks[11].direction == Blockdirection.右 &
                    Blocks[12].direction == Blockdirection.横 &
                    Blocks[13].direction == Blockdirection.左 &
                    Blocks[22].direction == Blockdirection.右 &
                    Blocks[23].direction == Blockdirection.横 &
                    Blocks[24].direction == Blockdirection.横 &
                    Blocks[25].direction == Blockdirection.左 &
                    Blocks[34].direction == Blockdirection.右 &
                    Blocks[35].direction == Blockdirection.左)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case 2 :
                    if (Blocks[1].direction == Blockdirection.左 &
                        Blocks[6].direction == Blockdirection.下 &
                        Blocks[7].direction == Blockdirection.横 &
                        Blocks[8].direction == Blockdirection.左 &
                        Blocks[10].direction == Blockdirection.右 &
                        Blocks[11].direction == Blockdirection.左 &
                        Blocks[13].direction == Blockdirection.下 &
                        Blocks[14].direction == Blockdirection.左 &
                        Blocks[15].direction == Blockdirection.竖 &
                        Blocks[17].direction == Blockdirection.竖 &
                        Blocks[20].direction == Blockdirection.右 &
                        Blocks[21].direction == Blockdirection.横 &
                        Blocks[22].direction == Blockdirection.上 &
                        Blocks[23].direction == Blockdirection.右 &
                        Blocks[24].direction == Blockdirection.上 &
                        Blocks[25].direction == Blockdirection.下 &
                        Blocks[26].direction == Blockdirection.上 &
                        Blocks[34].direction == Blockdirection.右 &
                        Blocks[35].direction == Blockdirection.左)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case 3:
                    if (
                        Blocks[1].direction == Blockdirection.横 &
                        Blocks[2].direction == Blockdirection.横 &
                        Blocks[3].direction == Blockdirection.左 &
                        Blocks[12].direction == Blockdirection.右 &
                        Blocks[13].direction == Blockdirection.左 &
                        Blocks[22].direction == Blockdirection.右 &
                        Blocks[23].direction == Blockdirection.左 &
                        Blocks[25].direction == Blockdirection.下 &
                        Blocks[26].direction == Blockdirection.左 &
                        Blocks[32].direction == Blockdirection.右 &
                        Blocks[33].direction == Blockdirection.横 &
                        Blocks[34].direction == Blockdirection.上 &
                        Blocks[35].direction == Blockdirection.竖
                        )
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case 4:
                    if (
                        Blocks[1].direction == Blockdirection.左 &
                        Blocks[4].direction == Blockdirection.下 &
                        Blocks[5].direction == Blockdirection.左 &
                        Blocks[10].direction == Blockdirection.竖 &
                        Blocks[11].direction == Blockdirection.下 &
                        Blocks[12].direction == Blockdirection.横 &
                        Blocks[13].direction == Blockdirection.上 &
                        Blocks[14].direction == Blockdirection.竖 &
                        Blocks[15].direction == Blockdirection.下 &
                        Blocks[16].direction == Blockdirection.左 &
                        Blocks[19].direction == Blockdirection.竖 &
                        Blocks[20].direction == Blockdirection.竖 &
                        Blocks[23].direction == Blockdirection.竖 &
                        Blocks[24].direction == Blockdirection.竖 &
                        Blocks[25].direction == Blockdirection.竖 &
                        Blocks[28].direction == Blockdirection.右 &
                        Blocks[29].direction == Blockdirection.上 &
                        Blocks[32].direction == Blockdirection.右 &
                        Blocks[33].direction == Blockdirection.上 &
                        Blocks[34].direction == Blockdirection.右 &
                        Blocks[35].direction == Blockdirection.左 
                        )
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                default:
                    return false;

            }
        }
        /// <summary>
        /// 判断游戏是否结束
        /// </summary>
        /// <param name="block">引发游戏结束的block</param>
        /// <returns>ture为结束</returns>
    }

}


       
    







        

    


            

